package ae.pcfc.etks.online.itworx.ehs.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ae.pcfc.etks.online.itworx.ehs.web.dao.WorkflowDAO;
import ae.pcfc.etks.online.itworx.ehs.web.facade.EHSFacade;
import ae.pcfc.etks.online.itworx.ehs.web.locator.ServiceLocator;

@Controller
@RequestMapping("/admin/general/client/report")
public class TestController {

	private ServiceLocator serviceLocator;
	private WorkflowDAO workFlow;
	private EHSFacade ehsFacade;
	
	
	public WorkflowDAO getWorkFlow() {
		return workFlow;
	}

	@Resource(name = "workflowDAO")
	public void setWorkFlow(WorkflowDAO workFlow) {
		this.workFlow = workFlow;
	}

	public ServiceLocator getServiceLocator() {
		return serviceLocator;
	}

	@Resource(name = "serviceLocator")
	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}


	/*@Transactional(readOnly = true)
	@RequestMapping(value = "/list.htm", method = RequestMethod.GET)
	public void list() {
		System.out.println("inside the list method in test controller!");
		try
		{
			List<Request> requests=serviceLocator.getFacadeService(WorkflowFacade.class).getAllRequests("1", "", UserType.User,null, null, null,null, false, 0);
			System.out.println("Size of results is: "+requests.size());
			if(requests.size()>0)
			{
				OFCRequest request= serviceLocator.getFacadeService(WorkflowFacade.class).getRequestDetails(requests.get(0).getId(),"");
				String executer=request.getExecuter().toString();
				
				System.out.println("OFC History size is "+request.getOfcHistory().size());
				
			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

		System.out.println("done listing");

	}*/
	
	@Transactional
	@RequestMapping(value = "/history.htm", method = RequestMethod.GET)
	public void listHistory() {
		System.out.println("inside the list method in test controller!");
		try
		{
			//List<Request> requests=serviceLocator.getFacadeService(WorkflowFacade.class).getAllRequests("1", "", UserType.User,null, null, null,0);
			//System.out.println("Size of results is: "+requests.size());
		//	if(requests.size()>0)
			//{
				/*OFCRequest request= serviceLocator.getFacadeService(WorkflowFacade.class).getRequestDetails(new Long(1),"");
				
				System.out.println("OFC History size is "+request.getOfcHistory().size());
				System.out.println("OFC History size is "+request.getOfcHistory().size());
				System.out.println("OFC History activity is "+request.getOfcHistory().get(0).getActivity());
				
				OFCRequestHistory history=new OFCRequestHistory();
				
				history.setCreationDate(new Date());
				history.setServiceId("1");
				history.setUserId("1");
				history.setExecuter(UserType.User);
				history.setBusinessUnit("BU");
				history.setRequestedBy("Ahmed");
				history.setImsLogId("1");
				history.setImsStatus("status");
				history.setLicenseNumber("11");
				history.setBcc("bb");
				history.setLocation("111");
				history.setMainEmail("email");
				//history.setCompany("company name");
				//history.setCompanyName("company name");
				history.setFees(30);
				history.setOfficerId("11");
				history.setProcessId("33");
				history.setType(RequestType.New);
				history.setActivity("activity");
				history.setStatus(RequestStatus.Submitted);*/
				
				
				//request.getOfcHistory().add(history);
				
				//workFlow.saveObject(request);
				
			//}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

		System.out.println("done listing");

	}
	
	@Transactional
	@RequestMapping(value = "/history2.htm", method = RequestMethod.GET)
	public void listHistory2() {
		System.out.println("inside the list method in test controller!");
		/*try
		{
			//List<Request> requests=serviceLocator.getFacadeService(WorkflowFacade.class).getAllRequests("1", "", UserType.User,null, null, null,0);
			//System.out.println("Size of results is: "+requests.size());
		//	if(requests.size()>0)
			//{
				OFCRequest request= serviceLocator.getFacadeService(WorkflowFacade.class).getRequestDetails(new Long(1),"");
				
				System.out.println("OFC officer ID is "+request.getOfficerId());
				
				
				
				//request.getOfcHistory().add(history);
				
				//workFlow.saveObject(request);
				
			//}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

		System.out.println("done listing");*/

	}
	
	@Transactional()
	@RequestMapping(value = "/create.htm", method = RequestMethod.GET)
	public void creation() {
		System.out.println("inside the creation method in test controller!");
		/*try
		{
			Service service=Service.ofcService;
//			service.setName("OFC");
//			service.setDescription("OFC Service");
			
			workFlow.saveObject(service);
			
			OFCRequest request=new OFCRequest();
			request.setCreationDate(new Date());
			request.setProcessingDate(new Date());
			request.setServiceId(service);
			request.setUserId("1");
			request.setExecuter(UserType.User);
			request.setBusinessUnit("BU");
			request.setRequestedBy("Ahmed");
			request.setImsLogId("1");
			request.setImsStatus("status");
			request.setLicenseNumber("11");
			request.setBcc("bb");
			request.setLocation("111");
			request.setMainEmail("somaya.rayan@itworx.com");
			request.setCompanyNameArabic("company name");
			request.setCompanyNameEnglish("company name");
			//request.setFees(30);
			request.setOfficerId("11");
			request.setProcessId("33");
			request.setType(RequestType.New);
			request.setActivity("activity");
			request.setStatus(RequestStatus.Resubmitted);
			
			
			OFCRequestHistory history=new OFCRequestHistory();
			
			history.setCreationDate(new Date());
			history.setServiceId("1");
			history.setUserId("1");
			history.setExecuter(UserType.User);
			history.setBusinessUnit("BU");
			history.setRequestedBy("Ahmed");
			history.setImsLogId("1");
			history.setImsStatus("status");
			history.setLicenseNumber("11");
			history.setBcc("bb");
			history.setLocation("111");
			history.setMainEmail("email");
			//history.setCompany("company name");
			//history.setCompanyName("company name");
			history.setFees(30);
			history.setOfficerId("11");
			history.setProcessId("33");
			history.setType(RequestType.New);
			history.setActivity("activity");
			history.setStatus(RequestStatus.Submitted);
			
			request.getOfcHistory().add(history);
			
			workFlow.createOFCRequest(request);		
			
			DocumentType documentType = new DocumentType();
			documentType.setEnglishName("englishNameupdated");
			documentType.setRemovable(true);
			try {
				serviceLocator.getFacadeService(EHSFacade.class).createNewDocumentType(documentType);
				System.out.println("Document Type is created");
				documentType.setEnglishDescription("Document uodated");
				System.out.println("Document Type is updated");
//				try {
//					serviceLocator.getFacadeService(EHSFacade.class).deleteDocumentType(serviceLocator.getFacadeService(EHSFacade.class).retrieveDocumentTypeByName("englishName").getId());
//					System.out.println("Document Type is deleted");
//				} catch (DocumentTypeNotFoundException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (InvalidInputException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			} catch (DocumentTypeAlraedyExistException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}*/

		System.out.println("done creation");

	}
}
